package com.example.h1nson.realphotography.viewpager;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.baidu.platform.comapi.map.C;
import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.adapter.FindAdapter1;
import com.example.h1nson.realphotography.adapter.FindAdapter2;
import com.example.h1nson.realphotography.asyncTask.FindAsynTask;
import com.example.h1nson.realphotography.asyncTask.HttpAsynTask;
import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.interface_photo.OnDataFinishedListener;
import com.example.h1nson.realphotography.interface_photo.OnFindBtnClickListener;
import com.example.h1nson.realphotography.interface_photo.OnFindItemListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by H1nson on 2016/5/4.
 */
public class MainTab03 extends Fragment {
    private ListView listView;
    private Button btn1,btn2;
    private boolean state;
    public FindAdapter1 findAdapter1;
    public FindAdapter2 findAdapter2;
    private List<Map> list = new ArrayList<>();
    private JSONObject jsonReq = new JSONObject();
    private LayoutInflater inflater1;
    private OnFindItemListener onFindItemListener;
    public MainTab03() {
    }
    public void setOnFindItemListener(OnFindItemListener onFindItemListener){
        this.onFindItemListener = onFindItemListener;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view =  inflater.inflate(R.layout.main_tab_03, container, false);
        listView = (ListView)view.findViewById(R.id.main_tab03_listview);
        inflater1 = inflater;
        getList();
        return view;
    }
    public void getList(){
        try {
            jsonReq.put("curUserId", GetCurrentUser.currentUser.getUserId());
        }catch (JSONException e){

        }

        FindAsynTask findAsynTask = new FindAsynTask(jsonReq);
        findAsynTask.setOnDataFinishedListener(new OnDataFinishedListener() {
            @Override
            public void onDataSuccessfully(Object data) {
                try {
                    JSONObject jsonObject = new JSONObject(data.toString());
                    for(int i = 0;i<jsonObject.getInt("size");i++){
                        Map map = new HashMap();
                        map.put("photoId",jsonObject.getInt("photoId"+i));
                        map.put("commenterName",jsonObject.getString("commenterName"+i));
                        map.put("commentContent",jsonObject.getString("comment"+i));
                        map.put("commentDate",jsonObject.getString("commentDate"+i));
                        list.add(map);
                    }
                    if(list.size()==0){

                    }else{
                        findAdapter1 = new FindAdapter1(inflater1,list);
                        System.out.println("commentList"+list.size()+"com"+list);
                        listView.setAdapter(findAdapter1);
                    }
                }catch (JSONException e){
                    e.printStackTrace();
                }
            }

            @Override
            public void onDataFailed() {

            }
        });
        findAsynTask.execute();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Map map = list.get(position);
                int photoId = (int)map.get("photoId");
                onFindItemListener.goDedtail(photoId);
            }
        });
    }
}